package app.boot;

import NextClass.NextClassAPI;
import app.exception.BaseException;
import vm.runtime.NextVMRuntime;

import java.io.File;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.util.regex.Pattern;

public class Main {

    public static PrintStream printStream = new PrintStream(System.out);
    public static String MyHelpInformation = "Next:\n" +
            "   -version            Show the version information.\n" +
            "   [script file]       Run the Next Script";
    public static Pattern pattern_split_expression = Pattern.compile(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)(?=([^\\(]*\\([^\\)]*\\))*[^\\)]*$)");
    public static double version = 3.0;
    public static String version_type = "Beta";
    public static String jarDirectory = null;
    static {
        try {
            //获取jar包的位置
            jarDirectory = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParent();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public static void JavaGC_Thread() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(10000);
                        System.gc();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        thread.start();
    }


    public static void main(String[] args) {
        try {
            int length = args.length;
            if (length == 0) {
                System.out.println(MyHelpInformation);
            } else {
                if (args[0].equals("-version")) {
                    System.out.println(version + " " + version_type);
                    System.exit(0);
                } else {
                    File target = new File(args[0]);
                    if (target.exists() && target.isFile() && target.canRead()) {
                        //设置新的运行环境 main
                        NextClassAPI.loadNativeClass();
                        NextVMRuntime vmRuntime = new NextVMRuntime("_init_");
                        vmRuntime.SourceFile = new File(target.getAbsolutePath());
                        try {
                            //System.out.println(new File("./").getAbsolutePath());
                            vmRuntime.code = BaseFunction.getScript(target.getAbsolutePath()).toArray(new String[0]); //把代码加载进入运行环境
                            vmRuntime.build();
                            long start = System.currentTimeMillis();
                            //JavaGC_Thread();
                            vmRuntime.run();
                            //System.exit(0);
                            long end = System.currentTimeMillis();
                            //System.out.println(end-start);
                        }
                        catch (Exception exception) {
                            //(exception).printStackTrace();
                            BaseException baseException = new BaseException(exception.getMessage() , 0 , true);
                            baseException.PrintErrorMessage();
                        }
                    } else {
                        System.out.println("[ERR] Can not start resource file: " + args[0]);
                        Runtime.getRuntime().exit(1);
                    }
                }
            }
        }catch (Exception e) {
            //e.printStackTrace();
            BaseException baseException = new BaseException(e.getMessage() , 0 , true);
            baseException.PrintErrorMessage();
        }
    }
}
